
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.operation.wave {
import org.goverla.collections.ArrayList;
import org.waveprotocol.wave.model.util.StringBuilder;
import org.waveprotocol.wave.model.wave.ParticipantId;

public class WaveletDelta {
	private var author:ParticipantId;
	
	/** List of operations */
	private var ops:ArrayList;
	
	/**
	 * Create new delta from an author and a list of operations.
	 *
	 * @param author of the operations
	 * @param ops list of operations
	 */
	public function WaveletDelta(author:ParticipantId, ops:ArrayList) {
		this.author = author;
		this.ops = ops;
	}
	
	public function getAuthor():ParticipantId {
		return author;
	}
	
	/**
	 * @return immutable list of the list of operations
	 */
	public function getOperations():ArrayList {
		return ops;
	}
	
	//  
	//  public function hashCode():int {
	//    // (see Effective Java)
	//    var result:int= 17;
	//
	//    result = 31* result + author.hashCode();
	//    var size:int= ops.length;
	//    for (var i:int=0; i< size; i++) {
	//    	var op:WaveletOperation= WaveletOperation(ops.get(i));
	//    	result = 31* result + op.hashCode();
	//    }
	//
	//    return result;
	//  }
	
	
	public function equals(obj:Object):Boolean {
		if (obj instanceof WaveletDelta) {
			var wd:WaveletDelta= WaveletDelta(obj);
			return author == (wd.author) && ops == (wd.ops);
		} else {
			return false;
		}
	}
	
	
	public function toString():String {
		var stringBuilder:StringBuilder= new StringBuilder();
		stringBuilder.append("WaveletDelta(" + author + ",");
		if (ops.length == 0) {
			stringBuilder.append("[]");
		} else {
			stringBuilder.append(" " + ops.length + " ops: [" + ops.getItemAt(0));
			for (var i:int= 1; i < ops.length; i++) {
				stringBuilder.append("," + ops.getItemAt(i));
			}
			stringBuilder.append("]");
		}
		stringBuilder.append(")");
		return stringBuilder.toString();
	}
}
}